package tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingDeque;

//二分搜索树
public class BST<E extends Comparable<E>> {

    private class Node {
        public E e;
        public Node left, right;

        public Node(E e) {
            this.e = e;
            left = null;
            right = null;
        }

    }

    private Node root;
    private int size;

    public BST() {
        root = null;
        size = 0;

    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void add(E e) {
       root=add(root,e);
    }

    //向已Node为根的二分搜索树中插入元素E，递归算法
    private  Node add(Node node, E e) {

        if(node==null){
            size++;
            return new Node(e);
        }
//        if (e.equals(node.e)) {
//            return;
//        } else if (e.compareTo(node.e) < 0 && node.left == null) {
//            node.left = new Node(e);
//            size++;
//            return;
//        } else if (e.compareTo(node.e) > 0 && node.right == null) {
//            node.right = new Node(e);
//            size++;
//            return;
//        }

        if (e.compareTo(node.e) < 0) {
           node.left= add(node.left, e);
        } else if(e.compareTo(node.e)>0) {
            node.right=  add(node.right, e);
        }
        return node;
    }

    //看二分搜索树是否包含元素e;
    public boolean contains(E e){
        return  contains(root,e);
    }
    //看已node为根的二分搜索树中是否包含元素e,递归算法
    private boolean contains(Node node,E e){
        if(node==null){
            return false;
        }
        if(e.compareTo(node.e)==0){
            return true;
        }
        else if(e.compareTo(node.e)<0){
            return contains(node.left,e);
        }else{
            return contains(node.right,e);
        }
    }
    //前序遍历
    public void preOrder(){
        preOrder(root);

    }
    private void preOrder(Node node){
        if(node==null){
            return;
        }
        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }


    //非递归前序遍历
    public void preOrderNR(){
        Stack<Node>  stack=new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            Node cur =stack.pop();
            System.out.println(cur.e);
            if(cur.right!=null){
                stack.push(cur.right);
            }
            if(cur.left!=null){
                stack.push(cur.left);
            }
        }
    }

    //中序遍历
    public void inOrder(){
        inOrder(root);

    }
    private void inOrder(Node node){
        if(node==null){
            return;
        }

        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }
    //后序遍历
    public void afterOrder(){
        afterOrder(root);

    }
    private void afterOrder(Node node){
        if(node==null){
            return;
        }
        afterOrder(node.left);
        afterOrder(node.right);
        System.out.println(node.e);
    }
    //广度优先遍历。使用队列
    public void levelOrder(){
        Queue<Node> queue=new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            Node cur= queue.remove();
            System.out.println(cur.e);
            if(cur.left!=null){
                queue.add(cur.left);
            }
            if(cur.right!=null){
                queue.add(cur.right);
            }
        }

    }




    //寻找最小元素
    public E minMum(){
        if(size==0){
            throw new IllegalArgumentException("BAT is Empty");
        }
        return  minMum(root).e;
    }
    public Node minMum(Node node){
        if(node.left==null){
            return node;
        }
        return minMum(node.left);

    }
    //寻找最大元素
    public E maxMum(){
        if(size==0){
            throw new IllegalArgumentException("BAT is Empty");
        }
        return  maxMum(root).e;
    }
    public Node maxMum(Node node){
        if(node.right==null){
            return node;
        }
        return maxMum(node.right);

    }

    //删除最小节点
    public E removeMin(){
        E ret=minMum();
        root = removeMin(root);
        return ret;
    }
    private  Node removeMin(Node node){
        if(node.left==null){
            Node rightNode=node.right;
            node.right=null;
            size--;
            return rightNode;
        }
        node.left=removeMin(node.left);
        size--;
        return node;
    }



    //从二分搜索树中删除元素为e的节点
    public void remove(E e){
        root =remove(root,e);
    }

    private Node remove(Node node,E e ){
        if(node==null){
            return  null;
        }
        if(e.compareTo(node.e)<0){
            node.left=remove(node.left,e);
            return node;
        }else if(e.compareTo(node.e)>0){
            node.right=remove(node.right,e);
            return node;
        }else {
            if(node.left==null){
                Node rightNode=node.right;
                node.right=null;
                size--;
                return rightNode;
            }

            if(node.right==null){
                Node lefNode=node.left;
                node.left=null;
                size--;
                return lefNode;
            }

            Node successor= minMum(node.right);
            successor.right=removeMin(node.right);
            successor.left=node.left;
            node.left=node.right=null;
            return  successor;
        }
    }


    @Override
    public String toString() {

        StringBuilder res=new StringBuilder();
        generateBSTString(root,0,res);
         return res.toString();
    }

    private void generateBSTString(Node root, int depth, StringBuilder res) {
        if(root==null){
            res.append(generateDepthString(depth)+"null\n");
            return;
        }
        res.append(generateDepthString(depth)+root.e+"\n");
        generateBSTString(root.left,depth+1,res);
        generateBSTString(root.right,depth+1,res );
    }

    private String generateDepthString(int depth){
        StringBuilder res=new StringBuilder();
        for(int i=0;i<depth;i++){
            res.append("--");
        }
        return res.toString();
    }
}
